package priv.wangy.micro.common.po.http.rspn;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import priv.wangy.micro.common.po.http.rspn.view.DefaultView;

import java.util.*;

/**
 * @author zhangjq
 * @since 2023-10-16
 */
@Data
@JsonView(DefaultView.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseListRspn<T> {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonView(DefaultView.class)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Field {

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        @JsonView(DefaultView.class)
        @JsonInclude(JsonInclude.Include.NON_NULL)
        public static class Property {

            /**
             * 配置项值
             */
            private Object value;

            /**
             * 配置项显示
             */
            private String display;

            @JsonIgnore
            public String getStrValue() {
                if (value instanceof String) {
                    return (String) this.value;
                }
                return null;
            }
        }

        /**
         * 配置项代码
         */
        private String code;

        /**
         * 配置项属性
         */
        private List<Property> properties;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonView(DefaultView.class)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Column {

        /**
         * column.prop
         */
        private String prop;

        /**
         * column.label
         */
        private String label;
    }

    /**
     * 总条数
     */
    private Integer total;

    /**
     * 内容
     */
    private List<T> content;

    /**
     * 配置项信息
     */
    private List<Field> fields;

    /**
     * 动态的列信息
     */
    private List<Column> columns;

    /**
     * 补充信息
     */
    private Map<String, Object> params;

    /**
     * 设置补充参数
     */
    public void setParam(String key, Object value) {
        if (params == null || params.isEmpty()) {
            params = new HashMap<>(8);
        }
        params.put(key, value);
    }

    @JsonAnyGetter
    public Map<String, Object> getParams() {
        return this.params;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void setTotal(long total) {
        this.total = (int) total;
    }

    public void addField(Field field) {
        if (Objects.isNull(this.fields)) {
            this.fields = new ArrayList<>();
        }
        this.fields.add(field);
    }
}
